package com.example.peibei.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.service.presenter.UploadImagePresenter;
import com.example.peibei.ui.utils.GlideEngine;
import com.huantansheng.easyphotos.EasyPhotos;
import com.huantansheng.easyphotos.callback.SelectCallback;
import com.huantansheng.easyphotos.models.album.entity.Photo;
import com.wildma.pictureselector.FileUtils;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;
import com.yxp.permission.util.lib.PermissionUtil;
import com.yxp.permission.util.lib.callback.PermissionResultCallBack;

import java.util.ArrayList;

import butterknife.BindView;

public class SendOrderActivity extends WDActivity {
    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_send_order)
    TextView tv_send_order;
    @BindView(R.id.et_order)
    EditText et_order;
    @BindView(R.id.iv_img_one)
    ImageView iv_img_one;
    @BindView(R.id.rl_add_one)
    RelativeLayout rl_add_one;
    @BindView(R.id.iv_img_two)
    ImageView iv_img_two;
    @BindView(R.id.rl_add_two)
    RelativeLayout rl_add_two;
    @BindView(R.id.iv_img_three)
    ImageView iv_img_three;
    @BindView(R.id.rl_add_three)
    RelativeLayout rl_add_three;

    String imagePathOne = "";
    String imagePathTwo = "";
    String imagePathThree = "";
    private UploadImagePresenter uploadImagePresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_send_order;
    }

    @Override
    protected void initView() {
        uploadImagePresenter = new UploadImagePresenter(new UploadCall());
        rl_add_one.setOnClickListener(v -> showChooseImageDialog());
        rl_add_two.setOnClickListener(v -> showChooseImageDialog());
        rl_add_three.setOnClickListener(v -> showChooseImageDialog());
        tv_send_order.setOnClickListener(v -> {
            String trim = et_order.getText().toString().trim();
            if(trim.equals("")){
                UIUtils.showToastSafe("内容不能为空");
                return;
            }
            String filePath = imagePathOne;
            if (!imagePathTwo.equals("")) {
                filePath = imagePathOne + "," + imagePathTwo;
            }
            if (!imagePathThree.equals("")) {
                filePath = imagePathOne + "," + imagePathTwo + "," + imagePathThree;
            }
            if(TextUtils.isEmpty(filePath)){
                UIUtils.showToastSafe("图片不能为空");
                return;
            }
            //跳转下一步
            Bundle bundle = new Bundle();
            bundle.putString("content",trim);
            bundle.putString("filePath",filePath);
            intent(SendOrderNextActivity.class,bundle);
            finish();
        });
    }

    private void showChooseImageDialog() {
        PictureSelector
                .create(SendOrderActivity.this, PictureSelector.SELECT_REQUEST_CODE)
                .selectPicture(true);
    }

    private static String[] PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA
    };

    private void getPermission()
    {
        PermissionUtil.getInstance().request(this,PERMISSIONS,
                new PermissionResultCallBack() {
                    @Override
                    public void onPermissionGranted() {


                        EasyPhotos.createAlbum(SendOrderActivity.this, false,true, GlideEngine.getInstance())
                                .start(new SelectCallback() {
                                    @Override
                                    public void onResult(ArrayList<Photo> photos, boolean isOriginal) {

                                        uploadImage(photos.get(0).path);
                                    }

                                    @Override
                                    public void onCancel() {

                                    }
                                });
                    }

                    @Override
                    public void onPermissionGranted(String... strings) {
                        // 当所有权限的申请被用户同意之后,该方法会被调用
                        //get_NovelUrl(id);
                    }

                    @Override
                    public void onPermissionDenied(String... permissions) {

                        UIUtils.showToastSafe("部分权限被禁止需要手动开启");

                        // 当权限申请中的某一个或多个权限,被用户曾经否定了,并确认了不再提醒时,也就是权限的申请窗口不能再弹出时,该方法将会被调用
                        getPermission();
                    }

                    @Override
                    public void onRationalShow(String... permissions) {
                        UIUtils.showToastSafe("部分权限被禁止需要手动开启");

                        // 当权限申请中的某一个或多个权限,被用户否定了,但没有确认不再提醒时,也就是权限窗口申请时,但被否定了之后,该方法将会被调用.
                        getPermission();

                    }

                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /*结果回调*/
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
            if (data != null) {
                PictureBean pictureBean = data.getParcelableExtra(PictureSelector.PICTURE_RESULT);
                if (pictureBean.isCut()) {
                    uploadImage(pictureBean.getPath());
                } else {
                    iv_img_one.setImageURI(pictureBean.getUri());
                }
            }
        }
    }

    private void uploadImage(String imagePath) {
        if (imagePathOne.equals("")) {
            rl_add_one.setVisibility(View.GONE);
            rl_add_two.setVisibility(View.VISIBLE);
            iv_img_one.setImageBitmap(BitmapFactory.decodeFile(imagePath));
        } else {
            if (imagePathTwo.equals("")) {
                rl_add_two.setVisibility(View.GONE);
                rl_add_three.setVisibility(View.VISIBLE);
                iv_img_two.setImageBitmap(BitmapFactory.decodeFile(imagePath));
            } else {
                rl_add_three.setVisibility(View.GONE);
                iv_img_three.setImageBitmap(BitmapFactory.decodeFile(imagePath));
            }
        }
        uploadImagePresenter.reqeust(imagePath);
    }

    class UploadCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            Log.i("answer", "图片上传成功：" + data);
            if (imagePathOne.equals("")) {
                imagePathOne = data;
            } else {
                if (imagePathTwo.equals("")) {
                    imagePathTwo = data;
                } else {
                    imagePathThree = data;
                }
            }

        }

        @Override
        public void fail(ApiException e, Object... args) {
//            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    @Override
    protected void destoryData() {
        FileUtils.deleteAllCacheImage(this);
    }
}
